package com.das.mechanic_base.widget;

import android.content.Context;
import android.graphics.Color;
import android.text.Editable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextWatcher;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.res.ResourcesCompat;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.das.mechanic_base.R;
import com.das.mechanic_base.app.X3Constent;
import com.das.mechanic_base.base.X3BaseBottomSheetDialog;
import com.das.mechanic_base.bean.greendao.AloneServiceListBean;
import com.das.mechanic_base.utils.X3DateUtils;
import com.das.mechanic_base.utils.LanguageUtiles;
import com.das.mechanic_base.utils.X3ScreenUtils;
import com.das.mechanic_base.utils.SpHelper;
import com.das.mechanic_base.utils.X3StringUtils;
import com.google.android.material.tabs.TabLayout;
import com.umeng.analytics.MobclickAgent;
import com.zyyoona7.wheel.WheelView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.List;

public class X3BottomMileOrTimeDialog extends X3BaseBottomSheetDialog implements ViewPager.OnPageChangeListener, View.OnClickListener, X3BaseBottomSheetDialog.IOnHideKey {

    private View v_line;
    private TabLayout tb_layout;
    private ViewPager vp_view;
    private TextView tv_btn;

    private String[] title;

    private List<View> fragList;
    private int lastIndex = -1;

    // 选择保养总里程
    private List<Integer> mileList;
    private AloneServiceListBean listBean;
    private String moneyUnit;
    private int currentMiles;
    private int recommendSafeMiles;
    private double perMiles;

    /**
     * 设置的新当前里程
     */
    private int newCurrentMiles;

    /**
     * 时间计算方式的保养总里程
     */
    private int newTimeCurrentMiles;

    /**
     * 按总里程计算的时间
     */
    private String nextMilesTime;

    /**
     * 按时间模式计算的时间
     */
    private String nextAllTime;

    /**
     * 选择保养时间 年
     */
    private List<Integer> yearList = new ArrayList<>();

    private String date;

    /**
     * 选择月份
     */
    private List<String> monthList = new ArrayList<>();
    private int month;
    private int year;
    private int currentDay;

    /**
     * 是否修改了保养里程
     */
    private boolean isSave = false;

    /**
     * 是否是检测
     */
    private boolean isAlone;
    private int end;

    public X3BottomMileOrTimeDialog(@NonNull Context context) {
        super(context);

    }

    @Override
    protected boolean getDialogCancel() {
        return false;
    }

    @Override
    protected void initView() {
        v_line = getView(R.id.v_line);
        tb_layout = getView(R.id.tb_layout);
        vp_view = getView(R.id.vp_view);
        tv_btn = getView(R.id.tv_btn);
        tv_btn.setOnClickListener(this);

        initMiles();

        this.setiOnHideKey(this);

        title = new String[]{mContext.getString(R.string.x3_mile_set_value),mContext.getString(R.string.x3_time_set_value)};

        for (int i = 0; i < title.length; i++) {
            tb_layout.addTab(tb_layout.newTab().setText(title[i]));
        }

        yearList = new ArrayList<>();
        Calendar c = Calendar.getInstance();
        year = c.get(Calendar.YEAR);
        month = c.get(Calendar.MONTH) + 1;
        currentDay = c.get(Calendar.DAY_OF_MONTH);

        for (int i = 0; i <= 50; i++) {
            yearList.add((year + i));
        }

        monthList = new ArrayList<>(Arrays.asList(mContext.getResources().getStringArray(R.array.select_month)));

        for (int i = 0; i < monthList.size(); i++) {
            String s = monthList.get(i);
            monthList.remove(i);
            monthList.add(i,s + " " + mContext.getString(R.string.plan_month));
        }

        moneyUnit = (String) SpHelper.getData(X3Constent.MILEAGEUNIT, "km");

        fragList = new ArrayList<>();
        fragList.add(View.inflate(mContext,R.layout.x3_fragment_mile_set_value,null));
        fragList.add(View.inflate(mContext,R.layout.x3_fragment_time_set_value,null));

        MileOrTimeAdapter mileOrTimeAdapter = new MileOrTimeAdapter(fragList,this);
        vp_view.setAdapter(mileOrTimeAdapter);
        vp_view.addOnPageChangeListener(this);

        tb_layout.setupWithViewPager(vp_view);
    }

    private void initMiles() {
        mileList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            mileList.add(i * 100);
        }

        for (int i = 1; i < 100; i++) {
            mileList.add(i * 1000);
        }

        for (int i = 10; i <= 100; i++) {
            mileList.add(i * 10000);
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.x3_bottom_mile_or_time_item;
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        startAnim(position);
    }

    /**
     * 背景白快动画
     * @param position
     */
    private void startAnim(int position) {
        if (lastIndex == position) {
            return;
        }
        Animation animation = v_line.getAnimation();
        if (animation != null) {
            animation.cancel();
        }

        end = 0;
        if (position == 0) {
            end = -X3ScreenUtils.dipToPx(6,mContext);
            v_line.animate().translationX(end).setDuration(200).start();
            Log.e("SSSS","开启动画：111111");
        }else {
            Log.e("SSSS","开启动画：222222");
            v_line.post(() -> {
                end = X3ScreenUtils.getScreenWidth(mContext) - v_line.getWidth() + X3ScreenUtils.dipToPx(6,mContext);
                v_line.animate().translationX(end).setDuration(200).start();
            });
        }

        lastIndex = position;
    }

    /**
     * 检测调整当前总里程
     * @param listBean
     */
    public void showCurrentMile(AloneServiceListBean listBean,String remindType) {
        this.isAlone = true;
        this.listBean = listBean;
        this.currentMiles = (int) listBean.getCurrentMiles();
        this.recommendSafeMiles = (int) listBean.getRecommendSafeMiles();
        this.perMiles = listBean.getPerMiles();
        this.newCurrentMiles = recommendSafeMiles;
        this.newTimeCurrentMiles = recommendSafeMiles;
        if ("TIME".equals(remindType)) {
            vp_view.setCurrentItem(1);
            //v_line.setTranslationX(ScreenUtils.getScreenWidth(mContext) - v_line.getWidth());
        }else {
            vp_view.setCurrentItem(0);
            //v_line.setTranslationX(-ScreenUtils.dipToPx(6,mContext));
        }
    }

    /**
     * 与h5联调 修复保养总里程
     */
    public void modifyCurrentMiles(double currentMile,double perMiles,double nextReplaceCycleKm,String remindType,String date) {
        this.isAlone = false;
        this.currentMiles = (int) currentMile;
        this.perMiles = perMiles;
        this.recommendSafeMiles = (nextReplaceCycleKm < currentMile) ? currentMiles : (int) nextReplaceCycleKm;
        this.newCurrentMiles = recommendSafeMiles;
        this.newTimeCurrentMiles = recommendSafeMiles;
        this.date = date;
        Log.e("SSSS","时间:" + date);
        if ("TIME".equals(remindType)) {
            vp_view.setCurrentItem(1);
        }else {
            vp_view.setCurrentItem(0);
        }
    }

    @Override
    public void dismiss() {
        if (v_line.getAnimation() != null) {
            v_line.getAnimation().cancel();
        }
        if (!isSave) {
            if (iOnAffirmSelect != null) {
                iOnAffirmSelect.iOnPickerDismiss();
            }
        }
        super.dismiss();
    }

    IOnAffirmSelect iOnAffirmSelect;

    public void setiOnAffirmSelect(IOnAffirmSelect iOnAffirmSelect) {
        this.iOnAffirmSelect = iOnAffirmSelect;
    }

    /**
     * 手动触发了 隐藏软键盘
     */
    @Override
    public void iOnHideKeyBoard() {

    }

    public interface IOnAffirmSelect {

        void iOnAffirmMiles(AloneServiceListBean sn);

        void iOnPickerDismiss();

    }

    IOnAffirmMile iOnAffirmMile;

    public void setiOnAffirmMile(IOnAffirmMile iOnAffirmMile) {
        this.iOnAffirmMile = iOnAffirmMile;
    }

    public interface IOnAffirmMile {
        void iOnAffirmMileAndTime(String time, String nextMiles, int type);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.tv_btn) {
            if (!isAlone) {
                if (vp_view.getCurrentItem() == 0) {
                    if (iOnAffirmMile != null) {
                        iOnAffirmMile.iOnAffirmMileAndTime(nextMilesTime, newCurrentMiles + "", 0);

                    }
                } else {
                    nextAllTime = nextAllTime.replace(" " + mContext.getString(R.string.plan_month), "");
                    if (iOnAffirmMile != null) {
                        iOnAffirmMile.iOnAffirmMileAndTime(nextAllTime, newTimeCurrentMiles + "", 1);
                    }
                }
                dismiss();
                return;
            }
            /**
             * 确定保养总里程
             */
            int currentItem = vp_view.getCurrentItem();
            int allMiles;
            if (currentItem == 0) {
                allMiles = newCurrentMiles;
            } else {
                allMiles = newTimeCurrentMiles;
            }
            if (allMiles < currentMiles) {
                allMiles = currentMiles;
            }

            isSave = true;

            /**
             * TODO 更新数据库中的剩余保养里程
             */
            listBean.setRecommendSafeMiles(allMiles);
            if (vp_view.getCurrentItem() == 0) {
                listBean.setReductionType("MILE");
                listBean.setWarningDate(X3DateUtils.getAloneSubmitTime(nextMilesTime));
            } else {
                listBean.setReductionType("TIME");
                nextAllTime = nextAllTime.replace(" " + mContext.getString(R.string.plan_month), "");
                listBean.setWarningDate(X3DateUtils.getAloneSubmitTime(nextAllTime));
            }

            isSave = true;

            try {
                dismiss();
                if (iOnAffirmSelect != null) {
                    iOnAffirmSelect.iOnAffirmMiles(listBean);
                }

            } catch (Exception e) {
                Log.e("SSSS", "错误：" + e.toString());
            }
        }
    }

    class MileOrTimeAdapter extends PagerAdapter {

        private List<View> mList;

        private X3BottomMileOrTimeDialog mileOrTimeDialog;
        private String monthindex;
        private int afterDay;
        private int afterDay1;
        private int afterDayTime1;

        public MileOrTimeAdapter(List<View> mList, X3BottomMileOrTimeDialog mileOrTimeDialog) {
            this.mList = mList;
            this.mileOrTimeDialog = mileOrTimeDialog;
        }

        @Override
        public int getCount() {
            return mList.size();
        }

        @Override
        public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
            return view == object;
        }

        @NonNull
        @Override
        public Object instantiateItem(@NonNull ViewGroup container, int position) {
            View view = mList.get(position);

            int remailCurrentMile = recommendSafeMiles - currentMiles;
            if (remailCurrentMile < 0) {
                remailCurrentMile = 0;
            }
            if(perMiles == 0.0 || perMiles == 0.00){
                afterDay = 0;
            }else {
                afterDay = X3StringUtils.getInt(remailCurrentMile / perMiles);
            }
            if (afterDay < 0) {
                afterDay = 0;
            }
            if (position == 0) {
                EditText et_input = view.findViewById(R.id.et_input);
                et_input.setCursorVisible(true);
                et_input.requestFocus();
                TextView tv_unit = view.findViewById(R.id.tv_unit);
                TextView tv_notice = view.findViewById(R.id.tv_notice);
                WheelView<Integer> wv_mile = view.findViewById(R.id.wv_mile);
                TextView tv_num = view.findViewById(R.id.tv_num);

                wv_mile.setTypeface(ResourcesCompat.getFont(mContext,R.font.pingfan_bold));
                wv_mile.setData(mileList);
                if (recommendSafeMiles < currentMiles) {
                    recommendSafeMiles = currentMiles;
                }

                et_input.setText(recommendSafeMiles + "");
                et_input.setSelection((recommendSafeMiles + "").length());
                tv_num.setText(recommendSafeMiles + "");
                tv_unit.setText(moneyUnit);

                int i = mileList.indexOf(recommendSafeMiles);
                if (i < 0) {
                    mileList.add(recommendSafeMiles);
                    Collections.sort(mileList);
                    i = mileList.indexOf(recommendSafeMiles);
                }
                wv_mile.setData(mileList);

                int selectedItemPosition = wv_mile.getSelectedItemPosition();
                if (selectedItemPosition != i) {
                    wv_mile.setSelectedItemPosition(i);
                }

                String addDaysMonth = X3DateUtils.getShowTime(date);

                if (afterDay == 0) {
                    String frontText = mContext.getString(R.string.x3_now_to_work);
                    String afterText = " | " + String.format(mContext.getString(R.string.x3_expect_miles_time),addDaysMonth);
                    SpannableString spannableString = new SpannableString(frontText + afterText);
                    //设置颜色
                    spannableString.setSpan(new ForegroundColorSpan(Color.parseColor("#F64444")), 0, frontText.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                    spannableString.setSpan(new ForegroundColorSpan(Color.parseColor("#707380")), frontText.length(), frontText.length() +afterText.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                    tv_notice.setText(spannableString);

                    nextMilesTime = year + "-" + ((month < 10) ? "0" + month : month);
                }else {

                    tv_notice.setTextColor(Color.parseColor("#707380"));
                    tv_notice.setText(String.format(mContext.getString(R.string.x3_remain_miles),remailCurrentMile + moneyUnit) + " | " +
                            String.format(mContext.getString(R.string.x3_expect_miles_time),addDaysMonth));

                    nextMilesTime = X3DateUtils.getSubmitTime(addDaysMonth);
                }

                wv_mile.setOnItemSelectedListener((wheelView, data, position1) -> {

                    newCurrentMiles = data;

                    String inputText = et_input.getText().toString();
                    if ((data + "").equals(inputText)) {
                        if (data < currentMiles) {
                            et_input.setText(currentMiles + "");
                            et_input.setSelection((currentMiles + "").length());
                            int i1 = mileList.indexOf(currentMiles);
                            if (i1 < 0) {
                                mileList.add(currentMiles);
                                Collections.sort(mileList);
                                i1 = mileList.indexOf(currentMiles);
                                wv_mile.setData(mileList);
                            }
                            wv_mile.setSelectedItemPosition(i1);

                            String frontText = mContext.getString(R.string.x3_now_to_work);
                            String afterText = " | " + String.format(mContext.getString(R.string.x3_expect_miles_time), X3DateUtils.getAddDaysMonth(0));
                            SpannableString spannableString = new SpannableString(frontText + afterText);
                            //设置颜色
                            spannableString.setSpan(new ForegroundColorSpan(Color.parseColor("#F64444")), 0, frontText.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                            spannableString.setSpan(new ForegroundColorSpan(Color.parseColor("#707380")), frontText.length(), frontText.length() + mContext.getString(R.string.x3_now_to_work).length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                            tv_notice.setText(spannableString);

                            nextMilesTime = year + "-" + ((month < 10) ? "0" + month : month);
                            return;
                        }else {
                            Log.e("SSSS","进入22222");
                            culteMile(data,tv_notice);
                            return;
                        }
                    }

                    if (data < currentMiles) {
                        Log.e("SSSS","进入333333:"+currentMiles+"::"+mileList.indexOf(currentMiles));
                        et_input.setText(currentMiles + "");
                        et_input.setSelection((currentMiles + "").length());
                        int i1 = mileList.indexOf(currentMiles);
                        if (i1 < 0) {
                            mileList.add(currentMiles);
                            Collections.sort(mileList);
                            i1 = mileList.indexOf(currentMiles);
                            wv_mile.setData(mileList);
                        }
                        wv_mile.setSelectedItemPosition(i1);

                        String frontText = mContext.getString(R.string.x3_now_to_work);
                        String afterText = " | " + String.format(mContext.getString(R.string.x3_expect_miles_time), X3DateUtils.getAddDaysMonth(0));
                        SpannableString spannableString = new SpannableString(frontText + afterText);
                        //设置颜色
                        spannableString.setSpan(new ForegroundColorSpan(Color.parseColor("#F64444")), 0, frontText.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                        spannableString.setSpan(new ForegroundColorSpan(Color.parseColor("#707380")), frontText.length(), frontText.length() + mContext.getString(R.string.x3_now_to_work).length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                        tv_notice.setText(spannableString);

                        nextMilesTime = year + "-" + ((month < 10) ? "0" + month : month);
                        return;
                    }

                    Log.e("SSSS","进入333333"+data);
                    et_input.setText(data + "");
                    et_input.setSelection((data + "").length());

                    culteMile(data,tv_notice);

                });

                mileOrTimeDialog.setiOnHideKey(() -> mileInputNum(et_input,wv_mile));

                et_input.setOnEditorActionListener((v, actionId, event) -> {
                    if (actionId == EditorInfo.IME_ACTION_DONE) {
                        mileInputNum(et_input,wv_mile);
                    }
                    InputMethodManager im = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
                    im.hideSoftInputFromWindow(et_input.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                    return true;
                });

                et_input.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                    }

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {

                    }

                    @Override
                    public void afterTextChanged(Editable s) {
                        tv_num.setText(s.toString());
                    }
                });

            }else {
                TextView tv_month = view.findViewById(R.id.tv_month);
                TextView tv_notice = view.findViewById(R.id.tv_notice);
                WheelView<Integer> wv_year = view.findViewById(R.id.wv_year);
                WheelView<String> wv_month = view.findViewById(R.id.wv_month);

                wv_year.setVisibleItems(6);
                wv_year.setTypeface(ResourcesCompat.getFont(mContext,R.font.pingfan_bold));
                wv_year.setData(yearList);

                wv_month.setVisibleItems(6);
                wv_month.setTypeface(ResourcesCompat.getFont(mContext,R.font.pingfan_bold));
                wv_month.setData(monthList);

                recommendSafeMiles = X3StringUtils.getInt(currentMiles + afterDay * perMiles);

                if (LanguageUtiles.isZhRCN()) {
                    if(X3StringUtils.isEmpty(date)){
                        wv_year.setSelectedItemPosition(yearList.indexOf(year));
                        wv_month.setSelectedItemPosition(monthList.indexOf((month < 10 ? "0"+month : month) + " " + mContext.getString(R.string.plan_month)));
                    }else {
                        String[] split = date.split("-");
                        if (split != null && split.length >= 2) {
                            wv_year.setSelectedItemPosition(yearList.indexOf(Integer.parseInt(split[0])));
                            wv_month.setSelectedItemPosition(monthList.indexOf(split[1] + " " + mContext.getString(R.string.plan_month)));
                        }
                        Log.e("SSSS","选中的"+ split[0] + "::" + split[1]);
                    }
                }else {
                    if(X3StringUtils.isEmpty(date)){
                        wv_year.setSelectedItemPosition(yearList.indexOf(year));
                        wv_month.setSelectedItemPosition(monthList.indexOf(month + " " + mContext.getString(R.string.plan_month)));
                        Log.e("SSSS","选中的asdkjfakl;f");
                    }else {
                        String[] split = date.split("-");
                        if (split != null && split.length >= 2) {
                            wv_year.setSelectedItemPosition(yearList.indexOf(Integer.parseInt(split[0])));
                            if (split[1].startsWith("0")) {
                                monthindex = split[1].substring(1, 2);
                            } else {
                                monthindex = split[1];
                            }
                            if(Integer.parseInt(monthindex) > 0){
                                wv_month.setSelectedItemPosition((Integer.parseInt(monthindex) - 1));
                            }
                        }
                    }
                }


                if (LanguageUtiles.isZhRCN()) {
                    nextAllTime = wv_year.getSelectedItemData() + "-" + wv_month.getSelectedItemData();
                }else {
                    for(int i = 0; i < monthList.size();i++){
                        if(monthList.get(i).equals(wv_month.getSelectedItemData())){
                            int a = i + 1;
                            nextAllTime = wv_year.getSelectedItemData() + "-" + (a < 10 ? "0"+ a : a);
                        }
                    }
                }
                wv_year.setOnItemSelectedListener((wheelView, data, position12) -> {
                    if (data == year) {
                        // 等于当前年的时候 需要判断选择的月份是否比当前月份小 如果小的话 需要将月份自动选择为当前月
                        int selectedItemPosition = wv_month.getSelectedItemPosition();
                        if (selectedItemPosition + 1 < month) {
                            wv_month.setSelectedItemPosition(month - 1);
                        }
                    }

                    if (LanguageUtiles.isZhRCN()) {
                        nextAllTime = wv_year.getSelectedItemData() + "-" + wv_month.getSelectedItemData();
                    }else {

                        for(int i = 0; i < monthList.size();i++){
                            if(monthList.get(i).equals(wv_month.getSelectedItemData())){
                                int a = i + 1;
                                nextAllTime = wv_year.getSelectedItemData()+"-"+ (a < 10 ? "0"+ a : a);
                            }
                        }
                    }
                });

                wv_month.setOnItemSelectedListener((wheelView, data, position13) -> {
                    int selectedItemPosition = wv_year.getSelectedItemPosition();
                    if (selectedItemPosition == 0) {
                        if (position13 + 1 < month) {
                            wv_month.setSelectedItemPosition(month - 1);
                        }
                    }
                     if (LanguageUtiles.isZhRCN()) {
                        nextAllTime = wv_year.getSelectedItemData() + "-" + wv_month.getSelectedItemData();
                     }else {
                         for(int i = 0; i < monthList.size();i++){
                             if(monthList.get(i).equals(wv_month.getSelectedItemData())){
                                 int a = i + 1;
                                 nextAllTime = wv_year.getSelectedItemData()+"-"+ (a < 10 ? "0"+ a : a);
                             }
                         }
                     }
                });
            }

            container.addView(view);
            return view;
        }

        /**
         * 总里程 输入框调整
         * @param et_input
         * @param wv_mile
         */
        private void mileInputNum(EditText et_input, WheelView<Integer> wv_mile) {
            String text = et_input.getText().toString();

            int currentIndex = 0;
            if (!X3StringUtils.isEmpty(text)) {
                int i = Integer.parseInt(text);
                if (i >= 1000000) {
                    i = 1000000;
                }
                if (i < currentMiles) {
                    i = currentMiles;
                }

                // 判断列表中是否有此项 没有的话自动添加
                if (!(i + "").equals(text)) {
                    et_input.setText(i + "");
                    et_input.setSelection((i + "").length());
                }

                currentIndex = mileList.indexOf(i);
                if (currentIndex < 0) {
                    mileList.add(i);
                    Collections.sort(mileList);
                    currentIndex = mileList.indexOf(i);
                    wv_mile.setData(mileList);
                }
                wv_mile.setSelectedItemPosition(currentIndex);
                Log.e("SSSS","获取输入后的值:" + i+ "::" + currentIndex + "::" + mileList.indexOf(i));
            }else {
                et_input.setText(currentMiles + "");
                int i = mileList.indexOf(currentMiles);
                if (i < 0) {
                    mileList.add(currentMiles);
                    Collections.sort(mileList);
                    i = mileList.indexOf(currentMiles);
                    wv_mile.setData(mileList);
                }
                wv_mile.setSelectedItemPosition(i);
            }
        }

        /**
         * 计算按照保养总里程
         * @param data
         * @param tv_notice
         */
        private void culteMile(Integer data, TextView tv_notice) {
            // 在这计算展示的数据
            int remainMiles = data - currentMiles;
            if (remainMiles < 0) {
                remainMiles = 0;
            }


            if(perMiles == 0.0){
                afterDay1 = 0;
                afterDayTime1 = 0;
            }else {
                afterDay1 = (int) (remainMiles / perMiles);
                afterDayTime1 = (int) Math.ceil(remainMiles / perMiles);
            }

            if (afterDay1 < 0) {
                afterDay1 = 0;
            }
            if (afterDayTime1 < 0) {
                afterDayTime1 = 0;
            }


            if (afterDay1 == 0) {
                String frontText = mContext.getString(R.string.x3_now_to_work);
                String afterText = " | " + String.format(mContext.getString(R.string.x3_expect_miles_time), X3DateUtils.getAddDaysMonth(0));
                SpannableString spannableString = new SpannableString(frontText + afterText);
                //设置颜色
                spannableString.setSpan(new ForegroundColorSpan(Color.parseColor("#F64444")), 0, frontText.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                spannableString.setSpan(new ForegroundColorSpan(Color.parseColor("#707380")), frontText.length(), frontText.length() + mContext.getString(R.string.x3_now_to_work).length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                tv_notice.setText(spannableString);

                nextMilesTime = year + "-" + ((month < 10) ? "0" + month : month);
            }else {
                tv_notice.setTextColor(Color.parseColor("#707380"));
                String addDaysMonth = X3DateUtils.getAddDaysMonth(afterDayTime1);
                tv_notice.setText(String.format(mContext.getString(R.string.x3_remain_miles),remainMiles + moneyUnit) + " | " +
                        String.format(mContext.getString(R.string.x3_expect_miles_time),addDaysMonth));
                nextMilesTime = X3DateUtils.getSubmitTime(addDaysMonth);
            }
        }

        @Override
        public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            container.removeView((View) object);
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return title[position];
        }

    }

    @Override
    protected void onStart() {
        super.onStart();
        MobclickAgent.onPageStart("双轨弹窗");
    }

    @Override
    protected void onStop() {
        super.onStop();
        MobclickAgent.onPageEnd("双轨弹窗");
    }
}
